<?php
namespace Home\Controller;
use Think\Controller;

/**
 * 登陆登出控制器
 */
class LoginController extends Controller
{

  public function index() {
    if (IS_POST) {
      $username = I('post.username');
      if (empty($username)) {
        $this->error(L('Please enter a username'));
      }
      

      
      // 查询用户记录
      $user = M('tbuserinformation')->field('nuserid,cpassword,cname')->where(array(
        'nuserid' => $username,
      ))->find();
      if (empty($user)) {
        $this->error(L('用户名不存在'));
      }
      if (!CheckPassword(I('post.password') , $user['cpassword'])) {
        $this->error(L('密码错误'));
      }
      
      // # 成功登陆
	session('user',$username);
      return $this->success(L('登陆成功') , U('User/index') , 0);
    }
    $this->display();
  }
  
  /**
   * 登陆系统
   */
  public function logout() {
    
    // 销毁session
    session('[destroy]');
    $this->success(L('Logout') , U('Login/login') , 0);
  }
}
